
#include "sys.h"
#include "uart.h"
#include "timer.h"

#include "norflash.h"


#define START_WIN_NUM_VP		0x1012 //启动界面上的数据变量显示控件的VP地址 

//因为数据变量显示控件的变量类型设置为了"长整数(4字节)",
//所以num变量的数据类型得设为u32,即也是4字节,保持一致
u16 num;
u16 open;
u8 sendbuf2[512];
u16 pageid ;
u16 is_beep;
u16 light;
u8 led_config_cmd;
u8 sleep_light;
u16 await_time;
u16 DATA_RAM;
u16 DATA_RAM1;

u16 tem;

u8 sn[8];
u8 sn2[8];
void main()
{





    UartInit(UART5, 19200);//串口初始化 uart5 485串口


    //定时器初始化
    T0_Init();
    T1_Init();

    StartTimer(3, 1000);//启动定时器3
    StartTimer(4, 1);//启动定时器4



    system_config(0, 2); // 关闭按键音


    Page_Change(0x32);//进入32页面



    //设置亮度
    led_config_cmd=5;
    read_dgus_vp(0x0082, (u8*)&DATA_RAM, 1);
    DATA_RAM &= 0x00FF;
    DATA_RAM1 = led_config_cmd;
    DATA_RAM1 <<= 8;
    DATA_RAM |= DATA_RAM1;
    write_dgus_vp(0x0082, (u8*)&DATA_RAM, 1);


		//初始化继电器IO
    SetPinOut(1, 1);
    SetPinOut(1, 2);
    SetPinOut(1, 3);
    SetPinOut(1, 4);
		
		
		//SN存储
		Nor_Flash_read(0x1700,sn,8);//读取falsh
		Uart_Send_Data(UART5, 8, sn); //发送485数据
		sn[0]=0x05;
		sn[1]=0x3D;
		sn[2]=0x00;
		sn[3]=0x00;
		sn[4]=0x00;
		sn[5]=0x00;
		sn[6]=0x00;
		sn[7]=0x01;
		Nor_Flash_write(0x1700,sn,8);//写入flash
		Nor_Flash_read(0x1700,sn2,8);
		Uart_Send_Data(UART5, 8, sn2); //发送485数据
		
    
		
		
		read_dgus_vp(0x1700, (u8*)(&sn2),8);//获取静音设置		
		Uart_Send_Data(UART5, 8, sn2); //发送485数据
		
		

    num = 5;
    while (1)
    {
        //delay_ms(1000);





        if(GetTimeOutFlag(4)) {
            StartTimer(4, 10);//启动定时器4

            //处理接收485数据
            if (Uart_Struct[UART5].rx_tail != Uart_Struct[UART5].rx_head) {

                if(Uart_Struct[UART5].rx_buf[Uart_Struct[UART5].rx_tail]=='\r')//如果接受到了"\r\n"结束标记,认为数据包接受完成
                {   Uart_Struct[UART5].rx_flag=1;
                }
                else if(Uart_Struct[UART5].rx_buf[Uart_Struct[UART5].rx_tail]=='\n')//如果接受到了'\n'结束标记,也认为数据包接受完成
                {
                    Uart_Struct[UART5].rx_flag=1;
                }

                Uart_Struct[UART5].rx_len++;
                Uart_Struct[UART5].rx_tail++;
                Uart_Struct[UART5].rx_tail &= SERIAL_COUNT;

            } else {
                if(Uart_Struct[UART5].rx_flag==0&&Uart_Struct[UART5].rx_len>0) {
                    Uart_Struct[UART5].rx_flag=1;
                    Uart_Struct[UART5].rx_tail=0;
                    Uart_Struct[UART5].rx_head=0;
                }
            }
        }


        if(GetTimeOutFlag(3)) {
            num++;//加1
            write_dgus_vp(START_WIN_NUM_VP,(u8*)&num,1);//设置某个DGUI地址的数据
            if(num%2==0) {
                open=1;
                write_dgus_vp(0x1011,(u8*)&open,1);//设置某个DGUI地址的数据
							
							PinOutput(1,1,1);//控制pin11打开
            } else {
                open=0;
                write_dgus_vp(0x1011,(u8*)&open,1);//设置某个DGUI地址的数据
							
							PinOutput(1,1,0);//控制pin11关
            }


            pageid = GetPageID();//获取当前页面Id
            read_dgus_vp(0x1105, (u8*)(&is_beep),1);//获取静音设置
						read_dgus_vp(0x0082, (u8*)&light, 1);//获取亮度
						read_dgus_vp(0x003e, (u8*)&tem, 1);//获取温度
						


            //接收485完成 处理数据
            if (Uart_Struct[UART5].rx_flag==1)
            {
                Uart_Send_Data(UART5,Uart_Struct[UART5].rx_len,Uart_Struct[UART5].rx_buf);

                Uart_Struct[UART5].rx_flag=0;
                Uart_Struct[UART5].rx_len=0;
            } else {



                sendbuf2[0]=pageid;//页面id
                sendbuf2[1]=is_beep;//获取静音设置
                sendbuf2[2]=light;//获取背光亮度
							  sendbuf2[3]=tem;//获取温度
							
                sendbuf2[4]=0xFF;
                Uart_Send_Data(UART5, 5, sendbuf2); //发送485数据

            }



            StartTimer(3, 1000);//再次 启动定时器3
        }














    }
}
